package com.jiatuxueyuan.the4.remoteio;

import io.netty.buffer.ByteBuf;

import java.util.Scanner;

public class TestTcpServer {

    public static void main(String[] args) throws Exception {

        TcpServer _server = new TcpServer("tcpNode1");

        Thread tcpThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    _server.start();
                } catch (Exception ignored) {
                }
            }
        });
        tcpThread.start();

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
            String command = scanner.nextLine();
            if("open".equals(command)) {
                ByteBuf openDO1 = new Zlan6842ModbusTcp((byte)0x01, (byte)0x05, 1, true).createPDU();
                _server.sendToClient("2856fb8750d6".toUpperCase(), openDO1);

                ByteBuf openDO2 = new Zlan6842ModbusTcp((byte)0x01, (byte)0x05, 2, true).createPDU();
                _server.sendToClient("2856fb8750d6".toUpperCase(), openDO2);
            } else if("close".equals(command)) {
                ByteBuf closeDO1 = new Zlan6842ModbusTcp((byte)0x01, (byte)0x05, 1, false).createPDU();
                _server.sendToClient("2856fb8750d6".toUpperCase(), closeDO1);

                ByteBuf closeDO2 = new Zlan6842ModbusTcp((byte)0x01, (byte)0x05, 2, false).createPDU();
                _server.sendToClient("2856fb8750d6".toUpperCase(), closeDO2);
            } else if("exit".equals(command)) {
                System.out.println("关闭服务端......，再见");
                _server.shutdown();
                break;
            }
            else {
                System.out.println("命令错误，只支持open和close、exit");
            }
        }
    }
}